package com.ruoyi.kis.controller;

import cn.hutool.http.HttpException;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult_KIS;
import com.ruoyi.kis.Vars.Constants;
import com.ruoyi.kis.domain.TltzdF;
import com.ruoyi.kis.service.IKisService;
import com.ruoyi.kis.util.HttpSendWmsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @Author: zhanglm
 * @CreateDate: 2025-03-04 14:59
 * @FullNameClass: com.ruoyi.kis.controller.KisZxController
 * @Description: 退料单
 *
 *
 */

@RestController
@RequestMapping("/kistl")
@Component("kistl")
@Api("kis退料通知单接口")
public class KisTltzController extends BaseController {



    @Autowired
    private IKisService kisService;

    private static final Logger LOGGER = LoggerFactory.getLogger(KisTltzController.class);


    /**
     *
     * @Description: 退料通知单 WMS >> ERP
     * @return ERP >> WMS
     */
    @ApiOperation("退料通知单 ")
    @RequestMapping("/tltzd")
    public AjaxResult_KIS tltzd(){
        List<TltzdF> tltzdFList = kisService.querytltzdF();
        String s  = "";
        if (tltzdFList != null && tltzdFList.size() > 0){
            s = JSONUtil.parse(tltzdFList.get(0)).toString();
            LOGGER.info("退料通知单 WMS >> ERP:JSON"+s);
            try {
//                String msg = HttpUtil.post(Constants.TLTZD_URL,s);
                String msg = HttpSendWmsUtil.sendWms(Constants.TLTZD_URL,s);
                JSONObject entries = JSONUtil.parseObj(msg);
                String msgcode = entries.get("msgcode").toString();
                String kisFlag ;
                if (msgcode.equals(Constants.ZHIKU.MSGCODE_SUCCESS)){
                    kisFlag = Constants.ZHIKU.KISFLAG_SUCCESS;
//                    int countUpdate = kisService.updateKisPOInstockFlag(tltzdFList.get(0).getFInterID(),Constants.ZHIKU.KISFLAG_SUCCESS);
                }else {
                    kisFlag = Constants.ZHIKU.KISFLAG_FAILURE;
                }
                int countUpdate = kisService.updateKisPOInstockFlag(tltzdFList.get(0).getFInterID(),kisFlag);
                LOGGER.info("退料通知单:{} WMS >> ERP:更新kisFlag状态:{},条数：{},反馈：{}",tltzdFList.get(0).getFBillNo(),kisFlag,countUpdate,msg);
            }catch (HttpException e){
                LOGGER.error("退料通知单反回失败信息：" + e.getMessage());
            }
        }
        return AjaxResult_KIS.success("OK","");
    }
}
